package com.zuxia.impDao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.zuxia.iDao.RecruitinfoDAOI;
import com.zuxia.po.Positioninfo;
import com.zuxia.po.Recruitinfo;
import com.zuxia.po.Staffindo;
import com.zuxia.util.PageModel;

/**
 * A data access object (DAO) providing persistence and search support for
 * Recruitinfo entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.zuxia.impDao.Recruitinfo
 * @author MyEclipse Persistence Tools
 */

public class RecruitinfoDAO extends HibernateDaoSupport implements RecruitinfoDAOI {
	private static final Logger log = LoggerFactory
			.getLogger(RecruitinfoDAO.class);
	// property constants
	public static final String RIICOUNT = "riicount";

	protected void initDao() {
		// do nothing
	}

	public void save(Recruitinfo transientInstance) {
		log.debug("saving Recruitinfo instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Recruitinfo persistentInstance) {
		log.debug("deleting Recruitinfo instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Recruitinfo findById(java.lang.Integer id) {
		log.debug("getting Recruitinfo instance with id: " + id);
		try {
			Recruitinfo instance = (Recruitinfo) getHibernateTemplate().get(
					"com.zuxia.impDao.Recruitinfo", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Recruitinfo instance) {
		log.debug("finding Recruitinfo instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.zuxia.impDao.RecruitinfoDAOI#findByProperty(java.lang.String, java.lang.Object)
	 */
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Recruitinfo instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Recruitinfo as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.zuxia.impDao.RecruitinfoDAOI#findByRiicount(java.lang.Object)
	 */
	public List findByRiicount(Object riicount) {
		return findByProperty(RIICOUNT, riicount);
	}

	/* (non-Javadoc)
	 * @see com.zuxia.impDao.RecruitinfoDAOI#findAll()
	 */
	public List findAll() {
		log.debug("finding all Recruitinfo instances");
		try {
			String queryString = "from Recruitinfo";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Recruitinfo merge(Recruitinfo detachedInstance) {
		log.debug("merging Recruitinfo instance");
		try {
			Recruitinfo result = (Recruitinfo) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Recruitinfo instance) {
		log.debug("attaching dirty Recruitinfo instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Recruitinfo instance) {
		log.debug("attaching clean Recruitinfo instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static RecruitinfoDAOI getFromApplicationContext(
			ApplicationContext ctx) {
		return (RecruitinfoDAOI) ctx.getBean("RecruitinfoDAO");
	}
	
	
	/**
	 * 张志鸿
	 */
	//添加方法
	public Boolean add(Recruitinfo recruitinfo) {
		Boolean b=true;
		log.debug("saving Recruitinfo instance");
		try {
			getHibernateTemplate().save(recruitinfo);
			log.debug("save successful");	
		} catch (RuntimeException re) {
			b=false;
			log.error("save failed", re);
			throw re;	
		}
		return b;
	}
	
	//重新写的根据ptIid查Recruitinfo
	public Boolean ptIidShowRecruitinfo(int ptIid) {
		Boolean b=false;
		log.debug("finding all Recruitinfo instances");
		try {
			String queryString = "from Recruitinfo where positioninfo.ptIid="+ptIid;
			if(getHibernateTemplate().find(queryString).size()>0)
			{
				b = true;
			}
		} catch (RuntimeException re) {
			b=false;
			log.error("find all failed", re);
			throw re;
		}
		return b;
	}

	public List<Recruitinfo> showRecruitinfo(String ptIname) {
		log.debug("show all Recruitinfo instances");
		try {
			List<Object[]> list=new ArrayList<Object[]>();
			String queryString = "select riid, rirequirement,riaddress,rientrynum,riicount,riibeginTime,riiendTime, positioninfo.ptIid,positioninfo.ptIname,positioninfo.ptIrequire  from Recruitinfo";
			if(ptIname!=null&&ptIname!="")
			{
				queryString = "select riid, rirequirement,riaddress,rientrynum,riicount,riibeginTime,riiendTime, positioninfo.ptIid,positioninfo.ptIname,positioninfo.ptIrequire  from Recruitinfo where positioninfo.ptIname like ?";
				Object[] value={"%"+ptIname+"%"};
				list=getHibernateTemplate().find(queryString,value);
			}
			else{
				list=getHibernateTemplate().find(queryString);
			}
			List<Recruitinfo> rlist=new ArrayList<Recruitinfo>();
			for(int i=0;i<list.size();i++)
			{
				Object[] obj=list.get(i); 
				Positioninfo p=new Positioninfo((Integer)obj[7], (String)obj[8], (String)obj[9]);
				Recruitinfo r=new Recruitinfo((Integer)obj[0],p,(String)obj[1],(String)obj[2],(Integer)obj[3],(Integer)obj[4],(Date)obj[5],(Date)obj[6]);
				rlist.add(r);
			}
		
			return rlist;
		} catch (RuntimeException re) {
			log.error("show all failed", re);
			throw re;
		}
	}
	
	//删除指定需求
	public Boolean deleteRecruitinfo(int riid){
		Boolean b=true;
		log.debug("deleting Recruitinfo instance");
		try {
			System.out.println("删除-----"+riid);
			Recruitinfo r=(Recruitinfo) getHibernateTemplate().getSessionFactory().openSession().get(Recruitinfo.class,riid);
			//在所关联的一方的set中移走当前要删除的对象
			r.getPositioninfo().getRecruitinfos().remove(r);
			getHibernateTemplate().delete(r);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			b=false;
			log.error("delete failed", re);
			throw re;
		}
		return b;
	}
	
	//查询一个Recruitinfo对象
	public Recruitinfo riidShowRecruitinfo(int riid){
		log.debug("getting Recruitinfo instance with id: " + riid);
		try {
			Recruitinfo instance = (Recruitinfo) getHibernateTemplate().getSessionFactory().openSession().get(Recruitinfo.class, riid);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	//修改需求
	public Boolean updateRecruitinfo(int riid, String riaddress, int riicount,
			Date riiendTime, String rirequirement) {
		// TODO Auto-generated method stub
		Boolean b=true;
		log.debug("update Recruitinfo instance");
		try {
			System.out.println("修改-----"+riid);
			Recruitinfo r=(Recruitinfo) getHibernateTemplate().getSessionFactory().getCurrentSession().get(Recruitinfo.class,riid);
			//在所关联的一方的set中移走当前要删除的对象
			r.setRiaddress(riaddress);
			r.setRiicount(riicount);
			r.setRiiendTime(riiendTime);
			r.setRirequirement(rirequirement);
			getHibernateTemplate().update(r);
			log.debug("update successful");
		} catch (RuntimeException re) {
			b=false;
			log.error("update failed", re);
			throw re;
		}
		return b;
	}
	
	public List<Recruitinfo> showPageModelList(String ptIname,final int pageNumber, final int limit) {
		//final Object[] values
		log.debug("showPageModelList"); 
		final StringBuffer hql = new StringBuffer("from Recruitinfo as a ");
		if(!ptIname.equals("")) {
			hql.append("where positioninfo.ptIname like "+"'%"+ptIname+"%'");
		}
		List list = getHibernateTemplate().executeFind(new HibernateCallback() {
			   
			   public Object doInHibernate(Session session) throws HibernateException,
			     SQLException {
			    Query query = session.createQuery(hql.toString());
			    //这个用于扩展多条件查询用到的
			   /* for (int i = 0; i < values.length; i++) {
			     query.setParameter(i, values[i]);
			    }*/
			    int p = pageNumber;
			    int l = limit;
			    int a = (p-1)*l;
			    List result = query.setFirstResult(a)
			        .setMaxResults(l)
			        .list();
			    /*List result = query
				        .list();*/
			    System.out.println("需求dao中查询到了数据"+result);
			    return result;
			   }
			  });
		
			  return list;
	}


	//分页查询
	public PageModel<Recruitinfo> showRecruitinfo(String ptIname,
			int pageNumber, int limit) {
		// TODO Auto-generated method stub
		List<Recruitinfo> list=this.showPageModelList(ptIname, pageNumber, limit);
		PageModel<Recruitinfo> p=new PageModel<Recruitinfo>(this.showRecruitinfo(ptIname).size(), pageNumber, limit);
		p.setList(list);
		return p;
	}
}
